Imports System.Collections.Generic
Partial Class Admin_RequiredFields
    Inherits System.Web.UI.Page

    Protected Sub SaveButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles SaveButton.Click
        Dim reqFieldList As List(Of Cdm.DataCore.Admin.AdminRequiredField)

        reqFieldList = Cdm.DataCore.Admin.AdminRequiredField.ListBySort(Convert.ToInt32(Me.DDLReqFields.SelectedValue), Me.RequiredFieldsScrollingGridView.SortExpression)
        For i As Integer = 0 To Me.RequiredFieldsScrollingGridView.Rows.Count - 1
            Dim chkBox As CheckBox
            chkBox = Me.RequiredFieldsScrollingGridView.Rows(i).FindControl("IsRequiredCheckBox")

            reqFieldList(i).IsRequired = chkBox.Checked
            reqFieldList(i).Persist()
        Next

        Me.RequiredFieldsObjectDataSource.Select()
        Me.RequiredFieldsScrollingGridView.DataBind()
        SetStatus("Settings Saved.")

    End Sub

    Private Sub SetupJavaScript()

        EntityController.JavaScriptAddChildDiv(Me.RequiredFieldsScrollingGridView.DivClientId, CInt(Me.RequiredFieldsScrollingGridView.ToolTip))
        ClientScript.RegisterStartupScript(Me.GetType(), "scrollDivs", EntityController.JavaScriptScrollingGridStartup(Me.RequiredFieldsScrollingGridView), True)
        EntityController.JavaScriptSetFixedHeight(28)
    End Sub

    Private Sub SetStatus(ByVal message As String)
        Me.RequiredFieldsScrollingGridView.StatusText = message
    End Sub
    Private Sub ClearStatus()
        SetStatus("")
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        ClearStatus()
        SetupJavaScript()
        If Not Page.IsPostBack Then
            Me.RequiredFieldsScrollingGridView.Sort("FieldDisplayName", SortDirection.Ascending)
        End If

        Me.DDLReqFields.Attributes.Add("onchange", "document.getElementById(""mainSection"").style.visibility=""hidden"";document.getElementById(""saveSection"").style.visibility=""hidden"";")
    End Sub



    Protected Sub RequiredFieldsScrollingGridView_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles RequiredFieldsScrollingGridView.DataBound
        Dim fieldName As String
        For i As Integer = 0 To Me.RequiredFieldsScrollingGridView.Rows.Count - 1
            Dim chkBox As CheckBox
            fieldName = CType(RequiredFieldsScrollingGridView.Rows(i).Cells(0).Controls(0), DataBoundLiteralControl).Text
            chkBox = Me.RequiredFieldsScrollingGridView.Rows(i).FindControl("IsRequiredCheckBox")

            If Not chkBox Is Nothing Then
                chkBox.InputAttributes.Add("title", "Is " & fieldName & " required?")
            End If
        Next
        AccessibilityController.AddImageToSortableGridHeaders(RequiredFieldsScrollingGridView)
    End Sub
End Class
